package com.tdkj.action.backstage.user;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.user.BeanUserExperiences;
import com.tdkj.model.user.BeanUserLevel;
import com.tdkj.service.user.IBeanUserLevelService;

public class UserLevelAction extends BaseAction {
	private static final long serialVersionUID = -2630704647668598130L;
	private IBeanUserLevelService userLevelService;
	private long id;
	private String levelname;
	private Integer startnumber;
	private Integer endnumber;
	private Timestamp createtime;
	private String levelcode;
	private List<BeanUserLevel> levels = new ArrayList<BeanUserLevel>();
	private BeanUserExperiences beanUserExperiences;
	public BeanUserLevel beanUserLevel;

	// 获取级别列表
	public String findAll() {
		// appendObj(3);
		// levels=userLevelService.findAll(BeanUserLevel.class);
		// return "findAll";

		Criteria criteria = this.createCriteria(BeanUserLevel.class);
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序

		this.queryByPage(criteria, "userLevelAction!findAll.action", 10000);
		// this.adminLogWrite("查询管理员列表");
		return "findAll";
	}

	/**
	 * 修改页面
	 * 
	 * @return
	 */
	public String modify() {
		beanUserLevel = this.getBaseService().findById(id, BeanUserLevel.class);
		return "modify";
	}

	/**
	 * 保存修改
	 * 
	 * @return
	 */
	public String saveModify() {
		BeanUserLevel beanUserLeveltemp=this.getBaseService().findById(beanUserLevel.getId(),BeanUserLevel.class);
		if(!beanUserLevel.getLevelname().equals(beanUserLeveltemp.getLevelname())){
			if(this.getBaseService().findByProperty("levelname", beanUserLevel.getLevelname(), BeanUserLevel.class).size()>0){
				this.addFieldError("nameErrorVlidate", "该级别名称已以存在");
				return modify();
			}
		}		
		
		this.getBaseService().saveOrUpdate(beanUserLevel, BeanUserLevel.class);
		this.adminLogWrite("成功修改了用户级别   用户级别ID:" + beanUserLevel.getId());
		return findAll();
	}

	/**
	 * 等级积分奖励页面
	 * 
	 * @return
	 */
	public String modifyIntegral() {
		beanUserLevel = this.getBaseService().findById(id, BeanUserLevel.class);
		beanUserExperiences = beanUserLevel.getBeanUserExperiences();
		return "modifyIntegral";
	}

	/**
	 * 保存等级积分奖励
	 * 
	 * @return
	 */
	public String saveIntegral() {
		this.getBaseService().saveOrUpdate(beanUserExperiences, BeanUserExperiences.class);
		this.adminLogWrite("成功修改了积分奖励  ID为:" + beanUserExperiences.getId());
		return findAll();
	}

	// 添加级别
	public String addLevel() {
		
		if(this.getBaseService().findByProperty("levelname", this.getLevelname(), BeanUserLevel.class).size()>0){
			this.addFieldError("nameErrorVlidate", "该级别名称已以存在");
			return findAll();
		}
		
		BeanUserExperiences beanUserExperiences = new BeanUserExperiences();
		userLevelService.save(beanUserExperiences);
		BeanUserLevel bean = new BeanUserLevel();
		bean.setLevelname(this.getLevelname());
		bean.setBeanUserExperiences(beanUserExperiences);
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		userLevelService.save(bean);
		return findAll();
	}

	// 删除级别
	public String deleteLevel() {
		BeanUserLevel bean = new BeanUserLevel();
		long id = this.getId();
		bean.setId(this.getId());
		userLevelService.delete(bean);
		return findAll();
	}

	private void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanUserLevel bean = null;
		for (int i = 0; i < num; i++) {
			bean = new BeanUserLevel();
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			bean.setLevelcode("code" + i);
			bean.setLevelname("用户等级" + i);
			userLevelService.save(bean);
		}
	}

	public IBeanUserLevelService getUserLevelService() {
		return userLevelService;
	}

	@Resource
	public void setUserLevelService(IBeanUserLevelService userLevelService) {
		this.userLevelService = userLevelService;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getLevelname() {
		return levelname;
	}

	public void setLevelname(String levelname) {
		this.levelname = levelname;
	}

	public Integer getStartnumber() {
		return startnumber;
	}

	public void setStartnumber(Integer startnumber) {
		this.startnumber = startnumber;
	}

	public Integer getEndnumber() {
		return endnumber;
	}

	public void setEndnumber(Integer endnumber) {
		this.endnumber = endnumber;
	}

	public Timestamp getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Timestamp createtime) {
		this.createtime = createtime;
	}

	public String getLevelcode() {
		return levelcode;
	}

	public void setLevelcode(String levelcode) {
		this.levelcode = levelcode;
	}

	public List<BeanUserLevel> getLevels() {
		return levels;
	}

	public void setLevels(List<BeanUserLevel> levels) {
		this.levels = levels;
	}

	public BeanUserLevel getBeanUserLevel() {
		return beanUserLevel;
	}

	public void setBeanUserLevel(BeanUserLevel beanUserLevel) {
		this.beanUserLevel = beanUserLevel;
	}

	public BeanUserExperiences getBeanUserExperiences() {
		return beanUserExperiences;
	}

	public void setBeanUserExperiences(BeanUserExperiences beanUserExperiences) {
		this.beanUserExperiences = beanUserExperiences;
	}

}
